import { createSlice, PayloadAction } from '@reduxjs/toolkit';

type UserState = {
  isLoggedIn: boolean;
  userInfo: {
    username: string;
  } | null;
};

const initialState: UserState = {
  isLoggedIn: false,
  userInfo: null
};

const userSlice = createSlice({
  name: 'user',
  initialState,
  reducers: {
    loginUser: (state, action: PayloadAction<{ username: string; password: string }>) => {
      state.isLoggedIn = true;
      state.userInfo = { username: action.payload.username };
    },
    registerUser: (state, action: PayloadAction<{ username: string; password: string }>) => {
      alert(`用户${action.payload.username}注册成功`);
    },
    logoutUser: (state) => {
      state.isLoggedIn = false;
      state.userInfo = null;
    }
  }
});

export const { loginUser, registerUser, logoutUser } = userSlice.actions;
export default userSlice.reducer;